function ENOB = FFTII(vIn)
% User: Armin Jalili
%
% Department of Electrical and Computer Engineering
% Isfahan University of Technology
% Updated by Armin Jalili
% Fri June 14 2013
%
nump = length(vIn);
w = hann(nump)';
doutw = vIn .* w;
dout_spec = fft(doutw);
dout_db = 20 * log10( abs ( dout_spec ) );
maxdb = max ( dout_db( 1 : fix (nump / 2 ) ) );
fin = find ( dout_db ( 1 : fix ( nump / 2)) == maxdb );
span = 5; %max ( fix ( nump / 200 ) , 5 );
spanh = 2;
specp = ( abs ( dout_spec ) ) .* ( abs ( dout_spec ) );
pdc = sum ( specp ( 1 : span ) );
ps = sum ( specp ( fin - span : fin + span ) );
ph = [];
fh = [];
hbin = [];
for har_num = 1 : 10
tone = rem ( ( har_num * (fin-1) + 1 ) / nump , 1 );
if tone > 0.5
tone = 1 - tone;
end
fh = [ fh tone ];
har_peak = max ( specp ( fix ( tone * nump ) - spanh : fix ( tone * nump ) + spanh ) );
har_bin = find ( specp ( fix ( tone * nump ) - spanh : fix ( tone * nump ) + spanh ) == har_peak );
har_bin = har_bin + fix ( tone *nump ) - spanh -1;
ph = [ ph sum( specp ( har_bin - 1 : har_bin + 1 ) ) ];
hbin = [ hbin har_bin ];
end
pd = sum ( ph ( 2 : 5 ) );
pn = sum ( specp ( 1 : fix ( nump / 2 ) ) ) - pdc - ps - pd;
sinad = 10 * log10 ( ps / ( pn + pd ) );
snr = 10 * log10 ( ps / pn );
thd = 10 * log10 ( pd / ph(1) );
SFDR = 10 * log10( ph(1) / max ( ph( 2 : 10 ) ) );
hd2 = 10 * log10 ( ph(2) / ph(1) );
ENOB = (sinad - 1.76) / 6.02;
plot([ 0 : fix(nump /2) - 1] .* 1 / nump, dout_db(1 : fix(nump / 2)) - maxdb);